package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.experiment.ExperimentMessage;
import com.siwei.mes.service.experiment.ExperimentMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 实验台账消息通知表相关
 *
 * @author makejava
 * @since 2024-07-26 22:13:25
 */
@Api(value = "实验台账消息通知表")
@RestController
@RequestMapping("experimentMessage")
public class ExperimentMessageController {
    @Resource
    private ExperimentMessageService experimentMessageService;

    @ApiOperation(value = "新增实验台账消息通知表")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) ExperimentMessage entity) {
        return Json.succ().data(ApiResult.addColumn("id", experimentMessageService.create(entity)));
    }

    @ApiOperation(value = "修改实验台账消息通知表")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ExperimentMessage entity) {
        experimentMessageService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除实验台账消息通知表")
    @PostMapping("/delete")
    public Json delete(@RequestBody ExperimentMessage entity) {
        experimentMessageService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取实验台账消息通知表")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ExperimentMessage, ExperimentMessage> query) {
        return Json.succ().data(experimentMessageService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取实验台账消息通知表")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(experimentMessageService.loadById(id));
    }

    @ApiOperation(value = "获取所有实验台账消息通知表")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ExperimentMessage entity) {
        return Json.succ().data(ApiResult.addList(experimentMessageService.getAll(entity)));
    }

    @ApiOperation("已读")
    @PostMapping("read")
    public Json read(@RequestBody ExperimentMessage entity) {
        assertTrue(entity.getId() == null, "实验台账消息通知id为空");
        experimentMessageService.read(entity.getId());
        return Json.succ().data(new ApiResult());
    }
}
